package com.epfly.promo.activity.dao;

import com.epfly.promo.activity.entity.UserActivityDinnerStatisticsEntity;
import org.apache.ibatis.annotations.*;
import tk.mybatis.mapper.additional.insert.InsertListMapper;
import tk.mybatis.mapper.common.BaseMapper;

import java.math.BigDecimal;

@Mapper
public interface UserActivityDinnerStatisticsMapper extends BaseMapper<UserActivityDinnerStatisticsEntity>, InsertListMapper<UserActivityDinnerStatisticsEntity> {


    @ResultMap(value = "userActivityDinnerStatisticsMapperBaseResultMap")
    @Select(value = "select total_amount, total_count from tbl_user_activity_dinner_statistics " +
            " where user_id = #{userId} and activity_id = #{activityId} and dinner_type = #{dinnerType} and del_flag = 0  ")
    UserActivityDinnerStatisticsEntity findByUserIdActivityIdAndDinnerType(@Param(value = "userId") String userId,
                                                                           @Param(value = "activityId") Long activityId,
                                                                           @Param(value = "dinnerType") Byte dinnerType);

    @Update(value = "update tbl_user_activity_dinner_statistics set on_way_amount = on_way_amount + #{discountAmount}, count = count + #{count} " +
            " where user_id = #{userId} and activity_id = #{activityId} and dinner_type = #{dinnerType} ")
    int increaseOnWayAmountAndOnWayCount(@Param(value = "userId") String userId, @Param(value = "activityId") Long activityId, @Param(value = "dinnerType") Byte dinnerType,
                                         @Param(value = "discountAmount") BigDecimal discountAmount, @Param(value = "count") int count);

    @Update(value = " update tbl_user_activity_dinner_statistics set total_amount = total_amount + #{discountAmount}, total_count = total_count + #{count} , " +
            " on_way_amount = on_way_amount - #{discountAmount}, on_way_count = on_way_count - #{count}" +
            " where user_id = #{userId} and activity_id = #{activityId} and dinner_type = #{dinnerType} " +
            " and on_way_amount >= #{discountAmount} and on_way_count >= #{count} ")
    int consumeOnWay(@Param(value = "userId") String userId, @Param(value = "activityId") Long activityId, @Param(value = "dinnerType") Byte dinnerType,
                     @Param(value = "discountAmount") BigDecimal discountAmount, @Param(value = "count") int count);


    @Update(value = "update tbl_user_activity_dinner_statistics set on_way_amount = on_way_amount - #{discountAmount}, on_way_count = on_way_count - #{count} " +
            " where user_id = #{userId} and activity_id = #{activityId} and dinner_type = #{dinnerType} " +
            "  and on_way_amount >= #{discountAmount} and on_way_count >= #{count} ")
    int decreaseOnWayAmountAndOnWayCount(@Param(value = "userId") String userId, @Param(value = "activityId") Long activityId, @Param(value = "dinnerType") Byte dinnerType,
                                         @Param(value = "discountAmount") BigDecimal discountAmount, @Param(value = "count") int count);

    @Update(value = " update tbl_user_activity_dinner_statistics set total_amount = total_amount - #{discountAmount}, total_count = total_count - #{count} , " +
            "  where user_id = #{userId} and activity_id = #{activityId} and dinner_type = #{dinnerType} " +
            "   and total_amount >= #{discountAmount} and total_count >= #{count} ")
    int rollback(@Param(value = "userId") String userId, @Param(value = "activityId") Long activityId, @Param(value = "dinnerType") Byte dinnerType,
                 @Param(value = "discountAmount") BigDecimal discountAmount, @Param(value = "count") int count);
}
